<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use app\admin\model\AuthGroupAccess;
use think\Db;
class Admin extends Common
{
	/**
     * 管理员列表
     * FunctionName: index
     * @author : roc
     */
    public function index(){
    	$modelAccess = new AuthGroupAccess;
        $data = $modelAccess->getAllData();
        $this->assign('data',$data);
        return view();
    }

    /**
     * 显示管理员添加修改视图
     * FunctionName: admin_info
     * @author : roc
     */
    public function admin_info(){
        $admin_id = input('route.admin_id')?input('route.admin_id'):0;
        $Group   = db('auth_group')->select();
        if($admin_id){
            $data = db('admin')->where("admin_id = $admin_id")->find();
            $group_data   = db('auth_group_access')->where(array('uid'=>$admin_id))->column('group_id');
        }
        $act = empty($admin_id) ? 'add' : 'edit';

        $assign = array('data'  => $data, 'group' => $Group, 'group_data'=>$group_data,'act'=>$act);
        $this->assign($assign);
        return view();
    }

    /**
     * 管理员添加修改删除方法
     * FunctionName: adminHandle
     * @author : roc
     */
    public function adminHandle(){
        $data = input('post.');
        $del  = input('route.');
        if($data['password1'] == '******' ){
            unset($data['password']);
        }else{
            $data['pwd'] = $data['password'];
            $data['password'] = md5($data['password']);
        }
        // $AdminModel = new Admin;
        // $AdminModel->startTrans();
        $group_ids = $data['group_ids'];
        Db::startTrans();
        try{
            if($data['act'] == 'edit'){
                $uid = $data['admin_id'];
                unset($data['admin_id']);
                unset($data['group_ids']);
                unset($data['password1']);
                unset($data['pass1']);
                unset($data['act']);
                $r = db('admin')->where('admin_id='.$uid)->update($data);
                $r === false ? $r = false : $r = true;
                /*********************记录日志***********************/
                if($r !== false){
                    $temp = db('admin')->where('admin_id='.$uid)->find();
                    addLog(session('user_name').'修改管理员成功-'.$temp['user_name'].' [ ID:'.$uid.' ]');
                }
            }

            if($del['delete'] == '1'){
                $uid = $del['admin_id'];
                $temp = db('admin')->where('admin_id='.$del['admin_id'])->find();
                $r = db('admin')->where('admin_id='.$del['admin_id'])->delete();
                /*********************记录日志***********************/
                if($r){
                    addLog(session('user_name').'删除管理员成功-'.$temp['user_name'].' [ ID:'.$uid.' ]');
                }
            }

            if($data['act'] == 'add'){
                unset($data['admin_id']);
                unset($data['group_ids']);
                unset($data['password1']);
                unset($data['pass1']);
                unset($data['act']);
                $data['add_time'] = time();
                if(db('admin')->where("user_name='".$data['user_name']."'")->count()){
                    $return_arr = array('status' => -1, 'msg'   => '此用户名已被注册，请更换', 'data'  => url('Admin/Admin/admin_info'));
                    echo json_encode($return_arr);exit;
                }else{
                    $uid = $r = db('admin')->insertGetId($data);
                    /*********************记录日志***********************/
                    addLog(session('user_name').'添加管理员成功-'.$data['user_name'].' [ ID:'.$uid.' ]');
                }
            }

            if($r != false || $r != 0){
                if($data['act'] != 'add'){
                    db('auth_group_access')->where(array('uid'=>$uid))->delete();
                }
                if (!empty($group_ids) AND $del['delete'] != '1') {
                    $dd = array('uid' => $uid,'group_id'=>$group_ids);
                    foreach ($dd as $k => $v) {
			            $dd[$k]=trim($v);
			        }
			        $id=db('auth_group_access')->insertGetId($dd);
                }
                Db::commit();
                $return_arr = array('status' => 1, 'msg'   => '操作成功', 'data'  => url('Admin/Admin/index'));
                echo json_encode($return_arr);exit;
            }else{
                $return_arr = array('status' => -1, 'msg'  => '操作失败', 'data'  => url('Admin/Admin/index'));
                echo json_encode($return_arr);exit;
            }
        }catch (Exception $e){
            Db::rollback();
            echo $e->getMessage();
        }
    }











}